import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


import org.springframework.mail.SimpleMailMessage;
import org.springframework.security.crypto.codec.Base64;




public class MailMain {
    public static void main(String[] args) {
//      final String username = "daviteq.test01@gmail.com";
//      final String password = "@!Abc123456";
//
//      Properties props = new Properties();
//      props.put("mail.smtp.auth", "true");
//      props.put("mail.smtp.starttls.enable", "true");
//      props.put("mail.debug", "true");
//      props.put("mail.smtp.host", "smtp.gmail.com");
//      props.put("mail.smtp.port", "587");
//
//      Session session = Session.getInstance(props,
//        new javax.mail.Authenticator() {
//          protected PasswordAuthentication getPasswordAuthentication() {
//              return new PasswordAuthentication(username, password);
//          }
//        });
//
//      try {
//
//          Message message = new MimeMessage(session);
//          message.setFrom(new InternetAddress(username));
//          message.setRecipients(Message.RecipientType.TO,
//              InternetAddress.parse("thanhthi.tran@daviteq.com"));
//          message.setSubject("Testing Subject");
//          message.setText("Dear Mail Crawler,"
//              + "\n\n No spam to my email, please!");
//
//          Transport.send(message);
//
//          System.out.println("Done");
//
//      } catch (MessagingException e) {
//          throw new RuntimeException(e);
//      }
      
      
//      ApplicationContext context = 
//              new ClassPathXmlApplicationContext("spring-mail.xml");
//      MailSenderManagement msm = (MailSenderManagement) context.getBean("mailSenderManagement");
//      msm.sendMail("Yong Mook Kim", "This is text content");
          
          MailSenderManagement msm = new MailSenderManagement();
          SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
          simpleMailMessage.setSubject("Testing base64 image");
          simpleMailMessage.setFrom("daviteq.test01@gmail.com");
//          simpleMailMessage.setTo("thanhthi.tran@daviteq.com");
    //      simpleMailMessage.setTo("uy.daonguyen@gmail.com");
    //      simpleMailMessage.setTo("uy.daonguyen@live.com");
          simpleMailMessage.setTo("thanhuy.nguyen@daviteq.com");
          StringBuilder bodyHTML = new StringBuilder();
         
          bodyHTML.append("<div><span>Send Mail image with base64 src</span></div>");
          bodyHTML.append("<br/>");
          File file = new File("E://test.jpg");
          try {
              FileInputStream fin = new FileInputStream(file);
              DataInputStream dis = new DataInputStream(fin);
              byte fileContent[] = new byte[(int)file.length()];
              dis.readFully(fileContent);
              
              //all chars in encoded are guaranteed to be 7-bit ASCII
              byte[] encoded = Base64.encode(fileContent);
              String encodedFile = new String(encoded);
              System.out.println(encodedFile);
              bodyHTML.append("<img src=\"data:image/jpg;base64,");
              bodyHTML.append(encodedFile);
              bodyHTML.append("\">");
              bodyHTML.append("<br/>");
              
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          bodyHTML.append("<br/>");
          bodyHTML.append("<div><span>End of mail</span></div>");
          simpleMailMessage.setText(bodyHTML.toString());
          
          CustomMailSender customMailSender = new CustomMailSender();
          msm.setMailSender(customMailSender);
          msm.sendMail("Dao Nguyen Thanh Uy", "Body content ",simpleMailMessage);
    }
}
